﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class PhongBanBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();

        public List<PHONGBAN> LayTatCa()
        {
            return DB.PHONGBANs.ToList();
        }

        public bool KiemTraTenPB(int maPB, string tenPB)
        {
            if (DB.PHONGBANs.Any(a => a.TenPB == tenPB && a.MaPB != maPB))
                return true;
            else
                return false;
        }

        public void ThemPB(PHONGBAN phongban)
        {
            DB.PHONGBANs.InsertOnSubmit(phongban);
            DB.SubmitChanges();
        }
        
        public PHONGBAN LayPhongBan(int maPB)
        {
            PHONGBAN pb = DB.PHONGBANs.Single(a => a.MaPB == maPB);
            return pb;
        }

        public string KiemTraTonTai(int maPB, string tenPB)
        {
            if (DB.PHONGBANs.Any(a => a.MaPB == maPB))
                return "Mã Phòng Ban";
            if (DB.PHONGBANs.Any(b => b.TenPB == tenPB))
                return "Tên Phòng Ban";
            else
                return null;
        }

        public void XoaPB(int mapb)
        {
            PHONGBAN pb = DB.PHONGBANs.Single(a => a.MaPB == mapb);
            DB.PHONGBANs.DeleteOnSubmit(pb);
            DB.SubmitChanges();
        }

        public void SuaPB(PHONGBAN pbn)
        {
            PHONGBAN pbo = DB.PHONGBANs.Single(a => a.MaPB == pbn.MaPB);
            pbo.TenPB = pbn.TenPB;
            pbo.NgayThanhLap = pbn.NgayThanhLap;
            pbo.MoTa = pbn.MoTa;
            DB.SubmitChanges();
        }

    }
}
